package com.huaxindata.im.main.contactview.friend;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxindata.common.map.HxDistanceUtil;
import com.huaxindata.im.R;
import com.huaxindata.im.main.manager.ContactManager;
import com.huaxindata.im.main.manager.UserManager;
import com.huaxindata.im.model.contact.Contact;
import com.huaxindata.im.model.contact.ContactPerson;

import java.util.List;

/**
 * 该adapter用于显示ContactPerson、ContactLocal、ContactGroup类
 * 要注意共用的时候区分好它们
 */
public class ContactListAdapter extends BaseAdapter {

    private Context        mContext;
    private LayoutInflater mInflater;

    /**
     * 这个集合不仅仅包含要展示的联系人， 还包括联系人按
     * 拼音字母开头进行分组的标题字符串，显示的时候要分开显示
     */
    private List<Object> mContactList;

    public ContactListAdapter(Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<Object> contactList) {
        this.mContactList = contactList;
        notifyDataSetChanged();
    }

    public List<Object> getData() {
        return mContactList;
    }

    @Override
    public int getCount() {
        return mContactList == null ? 0 : mContactList.size();
    }

    @Override
    public Object getItem(int position) {
        return mContactList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        Holder holder;
        if (view == null) {
            view = mInflater.inflate(R.layout.contact_list_item, parent, false);
            holder = new Holder(view);
        } else {
            holder = (Holder) view.getTag();
        }
        //默认是同时显示分组标题和内容的
        holder.mRootView.setVisibility(View.VISIBLE);
        holder.mCharTv.setVisibility(View.GONE);
        Object object = mContactList.get(position);
        //此adapter用于显示ContactPerson
        if (object instanceof String) {
            //如果是分组的标题，只显示分组标题
            holder.mCharTv.setText(object.toString());
            holder.mRootView.setVisibility(View.GONE);
            holder.mCharTv.setVisibility(View.VISIBLE);
        } else if (object instanceof Contact) {
            Contact contact = (Contact) object;
            holder.mNameTv.setText(contact.getNickName());
            holder.duty.setText("");

            HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
            instance.setHeadImage(mContext,contact.getIcon(), contact.getNickName(), holder.mIcon);

            //contact.showImageIcon(mContext, holder.mIcon, holder.mIconExpandTv);
            if (ContactManager.isFriend(contact.getClientId())) {
                holder.friend_icon.setVisibility(View.VISIBLE);
            } else {
                holder.friend_icon.setVisibility(View.GONE);
            }
            if (object instanceof ContactPerson) {
                ContactPerson person = (ContactPerson) contact;
                if (person.getLatLng() != null) {
                    String distanceStr = HxDistanceUtil.getDistanceStr(UserManager.getLatLng(),
                            person.getLatLng());
                    String duty = person.getDuty().trim();
                    if (!TextUtils.isEmpty(duty)) {
                        distanceStr = duty + "   " + distanceStr + "km";
                    }
                    holder.duty.setText(distanceStr);
                } else {
                    holder.duty.setText(person.getDuty());
                }
            }
        }
        return view;
    }

    private class Holder {
        AvatarImageView mIcon;
        TextView        mIconExpandTv;
        TextView        mNameTv;
        View            mRootView;
        TextView        mCharTv;
        public View     friend_icon;
        public TextView duty;

        public Holder(View view) {
            view.setTag(this);
            this.mIcon = (AvatarImageView) view.findViewById(R.id.contact_list_item_icon);
            this.mIconExpandTv = (TextView) view.findViewById(R.id.contact_list_item_icon_expand);
            this.mNameTv = (TextView) view.findViewById(R.id.contact_list_item_name);
            this.duty = (TextView) view.findViewById(R.id.contact_list_item_duty);
            this.mRootView = view.findViewById(R.id.contact_list_item_root);
            this.mCharTv = (TextView) view.findViewById(R.id.contact_list_item_char);
            this.friend_icon = view.findViewById(R.id.contact_list_item_friend_flag);
        }
    }

}
